var $ = layui.$;
var table = layui.table;
var layer = layui.layer;
var form = layui.form;

/*表格头部工具栏事件*/
table.on("toolbar(datalist)",function (obj){
    var event = obj.event;
    if ("check"==event){
        /**查询
         * 获取输入框中的内容
         * 根据输入框的内容查询结果
         * 表格重新加载数据
         * */
        var text = $("[name='checktext']").val();
        table.reload("datalist",{
            where:{checktext:text}
        });
    }
    else if ("add"==event){
        /**
         * 弹出一个对话框  让用户填写信息  点击保存按钮  保存数据  刷新表格  提示成功
         */
        form.val("addUserForm",{username:''});
        $.ajax({
            url:"../staff/initstafflist",
            async:false,
            success:function (result) {
                // result = JSON.parse(result);
                console.log(result);
                $("#windo [name='staff_id']").html("");
                for (var i = 0; i < result.length; i++) {
                    var item = result[i];
                    $("#windo [name='staff_id']")
                        .append("<option value='"+item.id+"'>"+item.name+"</option>")
                }{
                }
                form.render('select');
            }
        })
        layer.open({
            type: 1,
            title: '新增用户',
            shadeClose: false,
            // shade: true,
            maxmin: true, //开启最大化最小化按钮
            area: ['500px', '300px'],
            btn:["保存","重置"],
            // content: './index.html'
            content:$("#windo"),
            success:function (layero) {
                //让弹出层实现表单验证
                $(layero).addClass("layui-form");
                $(layero).find(".layui-layer-btn0").attr({"lay-submit":"lay-submit","lay-filter":"addUserSubmit"});
                form.on("submit(addUserSubmit)",function () {
                    //表单验证成功后执行
                    var formd = form.val("addUserForm");
                    console.log(formd)
                    //发送到后台
                    $.ajax({
                        url: "../user/add",
                        data: {
                            username: formd.username,
                            staff_id:formd.staff_id
                        },
                        success:function (){
                            console.log(formd)
                            // console.log(id)
                            table.reload("datalist");
                            layer.closeAll();
                            layer.msg("保存成功");
                        }
                    });
                })
            },
            btn1:function (){
                //点击按钮不要关闭对话框
                return false;
            }
        });
    }
});



table.on("tool(datalist)", function(obj) {
    console.log(obj)
    // var rowdata =
    var data = obj.data;
    //console.log(obj)
    if (obj.event == "del") {
        console.log("shanchu");
        layer.confirm('真的删除行么？', function (index) {
            //确定后要执行的代码
            obj.del();

            $.ajax({
                url: "../user/delete",
                data: {
                    id: data.id
                },
                success: function (res) {
                    console.log(res)
                    if (res==1){
                        layer.msg("删除成功", {icon: 1, time: 3000});
                        table.reload("datalist");
                    }else {
                        layer.msg("删除失败", {icon: 2, time: 3000});
                    }
                }
            })
            layer.close(index);
            // layer.msg("删除成功");
        });
    }else if ("edit"==obj.event){
        form.val("editUserForm",data);
        console.log("编辑");
        layer.open({
            type: 1,
            title: '修改用户名',
            shadeClose: false,
            // shade: true,
            maxmin: true, //开启最大化最小化按钮
            area: ['500px', '300px'],
            btn:["提交","重置"],
            // content: './index.html'
            content:$("#windoedit"),
            btn1:function (){
                //获取表单数据
                var formd = form.val("editUserForm");
                console.log(formd)
                console.log(777)
                //发送到后台
                $.ajax({
                    url: "../user/edit",
                    data: formd,
                    success:function (){
                        console.log(formd)
                        table.reload("datalist");
                        layer.msg("提交成功",{icon:1,time:3000});
                    }
                });
                layer.closeAll();
            }
        });
    }
});